﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using FileHelpers;
namespace DatabaseInsertAndExportLibrary
{
    public class InsertToDatabase
    {
        private string path;

        public InsertToDatabase(string path)
        {
            // TODO: Complete member initialization
            this.path = path;
        }
        private int callputconverter(string callput)
        {


            int cALL_PUT = 0;
            if (callput == "C")
            {
                cALL_PUT = 1;
            }
            else if (callput == "P")
            {
                cALL_PUT = 2;
            }

            return cALL_PUT;



        }
        public void insert()
        {
            string typeOfStock = Path.GetFileName(path).Substring(0, Path.GetFileName(path).Length - 10);
            if (typeOfStock == "hhif")
            {
                CSVAnalyser canalyse = new CSVAnalyser(this.path);
                canalyse.Analyse(canalyse.getstartindex(""), canalyse.getendindex("") - canalyse.getstartindex(""));
                FileHelperEngine engine = new FileHelperEngine(typeof(HsharesIndexFutures1Data));

                HsharesIndexFutures1Data[] hhif = engine.ReadFile(@"C:\temp\hhif120315fixed.csv") as HsharesIndexFutures1Data[];
                foreach (HsharesIndexFutures1Data cust in hhif)
                {

                    HKEXDatabaseDataSetTableAdapters.HsharesIndexFutures1TableAdapter hsharesIndexFutures1TableAdapter = new HKEXDatabaseDataSetTableAdapters.HsharesIndexFutures1TableAdapter();
                    hsharesIndexFutures1TableAdapter.Insert(cust.ContractMonth, cust.OpenPrice, cust.DailyHigh, cust.DailyLow, cust.SettlementPrice, cust.ChangeinSettlementPrice, cust.ContractHigh, cust.ContractLow, cust.Volume, cust.OpenInterest, cust.ChangeinOI);

                }

                canalyse.Analyse(canalyse.getstartindex(""), canalyse.getendindex("") - canalyse.getstartindex(""));
                FileHelperEngine enginehhif = new FileHelperEngine(typeof(HsharesIndexFutures2Data));
                HsharesIndexFutures2Data[] hhif2 = enginehhif.ReadFile(@"C:\temp\hhif120315fixedpart2.csv") as HsharesIndexFutures2Data[];
                foreach (HsharesIndexFutures2Data cust in hhif2)
                {
                    HKEXDatabaseDataSetTableAdapters.HsharesIndexFutures2TableAdapter hsharesIndexFutures2TableAdapter = new HKEXDatabaseDataSetTableAdapters.HsharesIndexFutures2TableAdapter();
                    hsharesIndexFutures2TableAdapter.Insert(cust.ContractMonth.ToString(), cust.null0, cust.Strategy, cust.null1, cust.null2, cust.OpenPrice, cust.DailyHigh, cust.DailyLow, cust.Volume);
                }
            }

            else if (typeOfStock == "hsif1")
            {
                CSVAnalyser canalyse = new CSVAnalyser(this.path);
                canalyse.Analyse(canalyse.getstartindex("\r\nContract Month,*Open Price,*Daily High,*Daily Low,Settlement Price,Change in Settlement Price,*Contract High,*Contract Low,Volume,Open Interest,Change in OI\r\n,,,,,,,,,,\r\n"), canalyse.getendindex("\r\n,,,,,,,,,,\r\n,,,,,,All Contracts Total,,") - canalyse.getstartindex("\r\nContract Month,*Open Price,*Daily High,*Daily Low,Settlement Price,Change in Settlement Price,*Contract High,*Contract Low,Volume,Open Interest,Change in OI\r\n,,,,,,,,,,\r\n"));
                FileHelperEngine enginehsif = new FileHelperEngine(typeof(HangSengIndexFutures1Data));

                HangSengIndexFutures1Data[] hsif = enginehsif.ReadFile(path.Substring(0, path.Length - 4) + "fixed.csv") as HangSengIndexFutures1Data[];
                foreach (HangSengIndexFutures1Data cust in hsif)
                {

                    HKEXDatabaseDataSetTableAdapters.HangSengIndexFutures1TableAdapter hangSengIndexFutures1TableAdapter = new HKEXDatabaseDataSetTableAdapters.HangSengIndexFutures1TableAdapter();
                    hangSengIndexFutures1TableAdapter.Insert(cust.ContractMonth, cust.OpenPrice, cust.DailyHigh, cust.DailyLow, cust.SettlementPrice, cust.ChangeinSettlementPrice, cust.ContractHigh, cust.ContractLow, cust.Volume, cust.OpenInterest, cust.ChangeinOI);
                }
                canalyse.Analyse(canalyse.getstartindex("\r\nContract Month,,Strategy,,,Open Price,Daily High,Daily Low,Volume,,\r\n,,,,,,,,,,\r\n"), canalyse.getendindex(",,,,,,All Strategies Total") - canalyse.getstartindex("\r\nContract Month,,Strategy,,,Open Price,Daily High,Daily Low,Volume,,\r\n,,,,,,,,,,\r\n"));
                FileHelperEngine enginehsif2 = new FileHelperEngine(typeof(HangSengIndexFutures2Data));
                HangSengIndexFutures2Data[] hsif2 = enginehsif2.ReadFile(path.Substring(0, path.Length - 4) + "fixed.csv") as HangSengIndexFutures2Data[];
                foreach (HangSengIndexFutures2Data cust in hsif2)
                {

                    HKEXDatabaseDataSetTableAdapters.HangSengIndexFutures2TableAdapter hangSengIndexFutures2TableAdapter = new HKEXDatabaseDataSetTableAdapters.HangSengIndexFutures2TableAdapter();
                    hangSengIndexFutures2TableAdapter.Insert(cust.ContractMonth.ToString(), cust.null0, cust.Strategy, cust.null1, cust.null2, cust.OpenPrice, cust.DailyHigh, cust.DailyLow, cust.Volume);
                }

            }
            else if (typeOfStock == "hsio")
            {
                CSVAnalyser canalyse = new CSVAnalyser(this.path);
                canalyse.Analyse(canalyse.getstartindex("\r\n\"CONTRACT MONTH\",\"STRIKE PRICE\",\"C/P\",\"OPENING PRICE\",\"DAILY HIGH\",\"DAILY LOW\",\"O.Q.P. CLOSE\",\"O.Q.P. CHANGE\",\"IV%\",\"VOLUME\",\"OPEN INTEREST\",\"CHANGE IN OI\"\r\n"), canalyse.getendindex(",,,,,,,\"TOTAL CALL\",") - canalyse.getstartindex("\r\n\"CONTRACT MONTH\",\"STRIKE PRICE\",\"C/P\",\"OPENING PRICE\",\"DAILY HIGH\",\"DAILY LOW\",\"O.Q.P. CLOSE\",\"O.Q.P. CHANGE\",\"IV%\",\"VOLUME\",\"OPEN INTEREST\",\"CHANGE IN OI\"\r\n"));
                FileHelperEngine enginehsio = new FileHelperEngine(typeof(HangSengIndexOptionsData));
                HangSengIndexOptionsData[] hsio = enginehsio.ReadFile(path.Substring(0, path.Length - 4) + "fixed.csv") as HangSengIndexOptionsData[];
                foreach (HangSengIndexOptionsData cust in hsio)
                {

                    HKEXDatabaseDataSetTableAdapters.HangSengIndexOptionsTableAdapter hangSengIndexOptionsTableAdapter = new HKEXDatabaseDataSetTableAdapters.HangSengIndexOptionsTableAdapter();
                    hangSengIndexOptionsTableAdapter.Insert(cust.ContractMonth, cust.STRIKEPRICE, callputconverter(cust.CallPut), cust.OPENINGPRICE, cust.DAILYHIGH, cust.DAILYLOW, cust.OQPCLOSE, cust.OQPCHANGE, cust.IV, cust.VOLUME, cust.OPENINGPRICE, cust.CHANGEINOI);
                    Console.WriteLine(hangSengIndexOptionsTableAdapter.GetData().ToString());
                }
            }
            else if (typeOfStock == "dqe")
            {
                CSVAnalyser canalyse = new CSVAnalyser(this.path);
                canalyse.Analyse(canalyse.getstartindex(",,,,,,,\r\nTOP 10 TRADED (BY VOLUME),,,,VOLUME,SETTLEMENT PRICE,IV%,HIGH#,LOW#,OPEN INTEREST,SETTLEMENT PRICE CHANGE**,% CHANGE IN SETTLEMENT PRICE\r\n,,,,,,,,,,,\r\n"), canalyse.getendindex("\r\n,,,,,,,,,,,\r\nTOP 5 UP (BY % CHANGE IN SETTLEMENT ") - canalyse.getstartindex(",,,,,,,\r\nTOP 10 TRADED (BY VOLUME),,,,VOLUME,SETTLEMENT PRICE,IV%,HIGH#,LOW#,OPEN INTEREST,SETTLEMENT PRICE CHANGE**,% CHANGE IN SETTLEMENT PRICE\r\n,,,,,,,,,,,\r\n"));
                FileHelperEngine enginehsiotop = new FileHelperEngine(typeof(HangSengIndexOptionsTop10Data));
                HangSengIndexOptionsTop10Data[] hsiotop = enginehsiotop.ReadFile(path.Substring(0, path.Length - 4) + "fixed.csv") as HangSengIndexOptionsTop10Data[];
                foreach (HangSengIndexOptionsTop10Data cust in hsiotop)
                {

                    HKEXDatabaseDataSetTableAdapters.HangSengIndexOptionsTop10TableAdapter hangSengIndexOptionsTop10TableAdapter = new HKEXDatabaseDataSetTableAdapters.HangSengIndexOptionsTop10TableAdapter();
                    hangSengIndexOptionsTop10TableAdapter.Insert(callputconverter(cust.CallPut), cust.ContractMonth, cust.STRIKEPRICE, cust.VOLUME, cust.OQPCLOSE, cust.IV, cust.DAILYHIGH, cust.DAILYLOW, cust.OPENINTEREST, cust.OQPCHANGE);

                }
            }

        }
    }
}
